<?php
/**
 * Created by 深圳市丹宏昊天电子商务有限公司.
 * 作 者: sqingwen
 * 日 期: 2016/12/15
 * 批量单列表页面
 */
?>
<link rel="stylesheet" href="<?= base_url() . 'resource/layui/css/layui.css' ?>" media="all">
<script type="text/javascript" src="<?= base_url() . 'resource/layui/layui.js' ?>" charset="utf-8"></script>

<style>
    s {
        font-style: normal
    }

    .layui-input, .layui-textarea {
        width: 93%;
    }

    .btnSearch {
        line-height: 35px;
        height: 35px;
        padding: 0 15px;
    }

</style>
<!--top结束-->
<div class="mian">
    <div class="mian1">
        <div class="mian2">
            <!--left start-->
            <?php $this->load->view('defaults/common/left'); ?>
            <!--left end-->

            <!--right start-->
            <div class="right">
                <!--breadcrumb start-->

                <!--breadcrumb end-->

                <!-- contents start -->
                <div id="contents">
                    <div class="panel panel-default">
                        <!-- Default panel contents -->
                        <div class="panel-heading">

                            <form action="" method="get">
                                <table>
                                    <tr>
                                        <td>
                                            <input name="searchOrderNum" class="layui-input" type="text"
                                                   placeholder="请输入维修单号/来货单号/SKU"
                                                   value="<?= isset($_GET['searchOrderNum']) ? $_GET['searchOrderNum'] : '' ?>">
                                        </td>

                                        <td>
                                            <button class="layui-btn btnSearch" lay-filter="formDemo" lay-submit="">
                                                立即查询
                                            </button>
                                        </td>
                                    </tr>
                                </table>


                            </form>

                            <div style="clear: both"></div>

                        </div>
                        <table class="layui-table" lay-skin="line">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>维修单号码</th>
                                <th>订单数量</th>
                                <th>来源站点</th>
                                <th>添加时间</th>
                                <th>到货百分比</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($list as $key => $val): ?>
                                <tr>
                                    <td><?= $val['id'] ?></td>
                                    <td><?= $val['order_num'] ?></td>
                                    <td><?= $val['quantity'] ?></td>
                                    <td><?= $val['point'] ?></td>
                                    <td><?= date('Y-m-d h:i:s' , $val['add_time']) ?></td>
                                    <td id="per_<?= $val['id'] ?>"
                                        style="<?= (($val['actual_quantity'] / $val['quantity']) > 1) ? 'color:#db3652;font-weight:bold;' : '' ?>">
                                        <?= number_format($val['actual_quantity'] / $val['quantity'] * 100 , 2) ?>%
                                    </td>
                                    <td>
                                        <button class="layui-btn layui-btn-primary shoow" data-id="<?= $val['id'] ?>"
                                                data-status="0" data-on="<?= $val['order_num'] ?>"
                                                data-qty="<?= $val['quantity'] ?>">
                                            <i class="layui-icon">&#xe60a;</i> <s>展开来货单</s>
                                        </button>


                                        <button class="layui-btn layui-btn-primary btn_add_waybill"
                                                data-on="<?= $val['order_num'] ?>" data-ci="<?= $val['id'] ?>"
                                                data-qty="<?= $val['quantity'] ?>">
                                            <i class="layui-icon">&#xe608;</i> <s>添加</s>
                                        </button>


                                    </td>
                                </tr>
                                <tr id="ship_<?= $val['id'] ?>" style="display:none;">
                                    <td colspan="2">&nbsp;</td>
                                    <td colspan="6">
                                        <div class="panel panel-default">
                                            <table class="table" id="waybill_tb_<?= $val['id'] ?>">
                                                <thead>
                                                <tr>
                                                    <th>序号</th>
                                                    <th>PID</th>
                                                    <th>来货单号</th>
                                                    <th>到货数量</th>
                                                    <th>到货时间</th>
                                                    <th style="display: none">机器识别码</th>
                                                    <th>到货百分比</th>
                                                    <th>操作</th>
                                                </tr>
                                                </thead>
                                                <tbody id="tb_waybills_<?= $val['id'] ?>">

                                                </tbody>
                                            </table>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <div style="clear:both">

                    </div>
                    <div style="width: 100%;" id="page">
                        <div style="float: left"><?= $show_params ?></div>
                        <div style="float:right;"><?= $show_pages ?></div>
                    </div>
                </div>
                <!-- contents end -->
            </div>
            <!--right end-->
            <div class="clear"></div>
        </div>
    </div>
</div>

<!-- 以下是各个弹出层的效果 begin ********************************************************************************************************* -->
<!-- 遮罩层 begin -->
<div id="overlay"></div>
<!-- 遮罩层 end -->

<!-- 添加货运单 begin -->
<div class="add_wayboll">
    <div style="width:100%;text-align: right;">
        <a href="javascript:void(0);" class="add_wayboll_close"><i class="layui-icon"
                                                                   style="font-size: 30px; color: #1E9FFF;color:#db3652;">
                &#x1006;</i></a></div>
    <fieldset class="layui-elem-field">
        <legend>添加来货单</legend>
        <div class="layui-field-box">
            维修单号：<span id="showOrderNum"></span>
        </div>
    </fieldset>
    <!-- Table -->
    <form class="form_waybill">
        <table class="layui-table" lay-skin="line">
            <tr>
                <td>
                    来货单号
                </td>
                <td>
                    到货数量
                </td>

                <td>
                    机器识别码
                </th>
            </tr>
            <tr>
                <td style="vertical-align: top;width:160px;">
                    <input type="text" class="layui-input" placeholder="来货单号" id="txtShipNum" name="txtShipNum">
                </td>
                <td style="vertical-align: top;width:80px;">
                    <input type="text" class="layui-input" placeholder="到货数量" id="txtShipQuantity"
                           name="txtShipQuantity" onkeyup="this.value=this.value.replace(/\D/g,'')"
                           onafterpaste="this.value=this.value.replace(/\D/g,'')">
                </td>

                <td>
                    <textarea class="layui-textarea" id="txtShipImeiNums" name="txtShipImeiNums"></textarea>
                </td>
            </tr>
            <tr style="display:none;">
                <td colspan="3">
                    <textarea class="layui-textarea" id="txtRemark" name="txtRemark"
                              placeholder="备注"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="text-align: right;">
                    <input type="hidden" value="0" id="hidOrderNum" name="hidOrderNum"/><!-- 维修单号 -->
                    <input type="hidden" value="0" id="hidContoinerId" name="hidContoinerId"/><!-- 维修单id -->
                    <input type="hidden" value="0" id="hidOrderQty" name="hidOrderQty"/><!-- 维修单预计数量 -->
                    <button class="btn btn-default btn-xs btnSubmittt" type="button">
                        打印条码
                    </button>

                </td>
            </tr>
        </table>
    </form>
</div>
<!-- 添加货运单 end -->

<!-- 拆分包裹 begin -->
<div class="main_unpk" id="main_unpk">
    <fieldset class="layui-elem-field" style="width: 90%;margin: 0 auto;">
        <legend>来货单拆分</legend>
        <div class="layui-field-box">
            拆分：<input type="text" name="btnUnpkQty" id="btnUnpkQty" placeholder="个数"
                      onkeyup="this.value=this.value.replace(/\D/g,'')"
                      onafterpaste="this.value=this.value.replace(/\D/g,'')" value="25"
                      style="width: 60px;height: 24px;">
            <button type="button" name="btnUnpkSave" id="btnUnpkSave" style="padding: 3px 8px;margin-left:20px;">拆包
            </button>
            <br/> 剩余：<input type="text" name="btnUnpkReQty" id="btnUnpkReQty" placeholder="剩余个数" value="0"
                            style="width: 60px;height: 24px;" disabled="disabled">
            <button type="button" name="btnUnpkReset" id="btnUnpkReset" style="padding: 3px 8px;margin-left:20px;">取消
            </button>
            <input type="hidden" name="hidPkQty" id="hidPkQty" value="0">
            <input type="hidden" name="hidPkId" id="hidPkId" value="0"><!-- 拆包单ID -->
            <input type="hidden" name="hidPkSort" id="hidPkSort" value="0"><!-- 拆包单排序 -->
            <input type="hidden" name="hidOnum" id="hidOnum" value="0"><!-- 维修单号 -->
            <input type="hidden" name="hidwNum" id="hidwNum" value="0"><!-- 来货单单号 -->
            <input type="hidden" name="hidGoid" id="hidGoid" value="0">
            <input type="hidden" name="hidGqty" id="hidGqty" value="0">
            <input type="hidden" name="hidUnpkum" id="hidUnpkum" value="0">
        </div>
    </fieldset>

</div>
<!-- 拆分包裹 end -->

<!-- 在列表里面的弹出的合并包裹弹出层 begin -->
<div class="main_merge" id="main_merge">
    <fieldset class="layui-elem-field" style="width: 90%;margin: 0 auto;">
        <legend>合并包裹</legend>
        <div class="layui-field-box">
            <p><label style="letter-spacing: 9px;font-weight: normal;">合并</label>：<input type="text" name="btnMergeQty"
                                                                                         id="btnMergeQty"
                                                                                         placeholder="个数"
                                                                                         disabled="disabled" value="25"
                                                                                         style="width: 50px;height: 24px;">
                <label style="letter-spacing: 9px;font-weight: normal;">剩余</label>：<input type="text"
                                                                                          name="btnMergeReQty"
                                                                                          id="btnMergeReQty"
                                                                                          placeholder="剩余个数"
                                                                                          value="0"
                                                                                          disabled="disabled"
                                                                                          style="width: 50px;height: 24px;">
            </p>
            <p style=" margin: 10px 0px;">
                包裹号：<input type="text" name="txtMergeNum" id="txtMergeNum" placeholder="合并包裹号" value=""
                           style="width: 160px;height: 24px;" disabled="disabled"></p>
            </p>
            <p>
                <button type="button" name="btnMergeSave" id="btnMergeSave" style="padding: 3px 8px;margin-left:20px;">
                    合并
                </button>
                <input type="hidden" name="hidMMQty" id="hidMMQty" value="0">
                <input type="hidden" name="hidMMId" id="hidMMId" value="0">
                <input type="hidden" name="hidMMNum" id="hidMMNum" value="0">
                <button type="button" name="btnMergeReset" id="btnMergeReset"
                        style="padding: 3px 8px;margin-left:20px;">取消
                </button>
            </p>
        </div>
    </fieldset>

</div>
<!-- 在列表里面的弹出的合并包裹弹出层 end -->
<!-- 以上是各个弹出层的效果 end** ********************************************************************************************************* -->

<script>
    //Demo
    layui.use('form', function () {
        var form = layui.form();
    });
    layui.use('element', function () {
        var element = layui.element();
    });
</script>
<style type="text/css">

    #page {
        padding: 7px 0;
    }

    #page a {

    }

    #page a .current {

    }

    .add_wayboll {
        display: none;
        width: 700px;
        background: #fff;
        padding: 13px 21px;
        box-shadow: 0 0 3px #000;
        position: fixed;
        top: 50%;
        left: 50%;
        margin: -201px 0 0 -351px;
        z-index: 110;
    }

    /*合并包裹 begin*/
    .merge_package_div {
        display: none;
        width: 700px;
        background: #fff;
        padding: 13px 21px;
        box-shadow: 0 0 3px #000;
        position: fixed;
        top: 50%;
        left: 50%;
        margin: -201px 0 0 -351px;
        z-index: 110;
        height: 450px;
        overflow: hidden;
    }

    #table_merge_package td, #table_merge_package th {
        padding: 5px 15px;
    }

    input[type=checkbox] {
        vertical-align: middle;
    }

    .cbkWaybill {
        display: none;
    }

    .layui-tab {
        margin: -16px 0;
    }

    #main_merge {
        width: 300px;
        height: 165px;
        position: absolute;
        border: 1px solid #666;
        background: #fff;
        display: none;
        border-radius: 2px;
        box-shadow: 2px 2px 1px #888;
    }

    /*合并包裹 end*/

    /*拆分包裹 begin*/
    .main_unpk {
        width: 246px;
        height: 120px;
        position: absolute;
        border: 1px solid #666;
        background: #fff;
        display: none;
        border-radius: 2px;
        box-shadow: 2px 2px 1px #888;
    }

    /*拆分包裹 end*/
    /* 半透明的遮罩层 */
    #overlay {
        background: #000;
        filter: alpha(opacity=50); /* IE的透明度 */
        opacity: 0.5; /* 透明度 */
        display: none;
        position: absolute;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 100; /* 此处的图层要大于页面 */
        display: none;
    }

    .layui-tab-content {
        border: 1px solid #e2e2e2;
        border-top: none;
    }
</style>
<script type="text/javascript">
    $('#txtShipNum1').keyup(function () {
        if ($.trim($('#txtShipNum').val()).length > 5) {
            $.ajax({
                type: "POST",
                async: false,
                url: '<?= blang_url('orders/Multi/ajaxCheckNum')?>',
                data: {'nt': 'waybillNum', 'n': $.trim($('#txtShipNum').val())},
                dataType: 'json',
                success: function (data) {
                    if (data.cnt == 0) {
                        layer.tips('该单号可以使用！', '#txtShipNum', {tips: [3, '#008cd7']});
                        $('.btnSubmittt').removeAttr("disabled");
                    } else {
                        layer.tips('该单号已经存在！', '#txtShipNum', {tips: [3, '#008cd7']});
                        $('.btnSubmittt').attr("disabled", true);
                    }
                }
            });
        } else {
            layer.tips('来货单号格式不正确！', '#txtShipNum', {tips: [3, '#008cd7']});

        }
    });

    $('.btnSubmittt').click(function () {
        var cid = $.trim($('#hidContoinerId').val());//容器id
        var oqty = $.trim($('#hidOrderQty').val());//订单总数
        var uri = '<?= blang_url('orders/Multi/ajaxAddWaybill')?>';
        var num = $('#txtShipNum').val();//来货单号
        var qty = $.trim($('#txtShipQuantity').val());//到货数量
        var imeis = $.trim($('#txtShipImeiNums').val());//机器识别码
        var remark = $.trim($('#txtRemark').val());//机器识别码
        var orderNum = $.trim($('#hidOrderNum').val());//维修单号


        if (num == '') {
            layer.tips('来货单号格式不正确！', '#txtShipNum', {tips: [3, '#008cd7']});
            $('#txtShipNum').focus();
            return;
        }
        if (qty == '' || qty == 0) {
            layer.tips('亲，没有到货数量哦！', '#txtShipQuantity', {tips: [3, '#008cd7']});
            $('#txtShipQuantity').focus();
            return;
        }
        if ($.trim($('#txtShipNum').val()).length <= 5) {
            layer.tips('来货单号格式不正确！', '#txtShipNum', {tips: [3, '#008cd7']});
            return;
        } else {
            $.ajax({
                type: "POST",
                async: false,
                url: '<?= blang_url('orders/Multi/ajaxCheckNum')?>',
                data: {'nt': 'waybillNum', 'n': $.trim($('#txtShipNum').val())},
                dataType: 'json',
                success: function (data) {
                    if (data.cnt == 0) {

                        $.ajax({
                            type: "POST",
                            url: uri,
                            async: false,
                            data: {
                                'num': num,
                                'qty': qty,
                                'imeis': imeis,
                                'orderNum': orderNum,
                                'remark': remark
                            },
                            dataType: "text",
                            success: function (data) {
                                layer.msg("添加成功！");
                                hideOverlay();//关闭遮罩层
                                $('.add_wayboll').hide();//关闭弹出层
                                clearAddDiv();//清理输入控件残余值
                                loadWaybill(orderNum, cid, oqty);//重新ajax加载来货单
                                $('#per_' + cid).text();
                            }
                        });

                    } else {
                        layer.tips('该单号已经存在！', '#txtShipNum', {tips: [3, '#008cd7']});
                        return;
                    }
                }
            });
        }


    });


    $('.shoow').click(function () {
        var id = $(this).attr('data-id');
        var status = $(this).attr('data-status');
        var orderNum = $(this).attr('data-on');//订单编号
        var qty = $(this).attr('data-qty');//预计订单总量
        if (status == 0) {
            $('#ship_' + id).show();
            $(this).attr('data-status', '1');
            $(this).find('s').text('关闭来货单');
            loadWaybill(orderNum, id, qty);//ajax加载来货单
        } else {
            $('#ship_' + id).hide();
            $(this).attr('data-status', '0');
            $(this).find('s').text('展开来货单');
        }

    });


    $('.show_code').click(function () {

    });

    //以下是添加货运单的js代码
    $('.btn_add_waybill').click(function () {
        var orderNum = $.trim($(this).attr('data-on'));
        var cid = $.trim($(this).attr('data-ci'));
        var oqty = $(this).attr('data-qty');//订单总量

        var persent = $.trim($('#per_' + cid).text());

        persent = persent.replace('%', '');
        if (persent >= 100) {
            layer.msg('已经满了');
            return;
        }
        showOverlay();//打开遮罩层
        $('.add_wayboll').show();
        $('#showOrderNum').text(orderNum);
        $('#hidOrderNum').val(orderNum);
        $('#hidContoinerId').val(cid);
        $('#hidOrderQty').val(oqty);
    });


    $('.add_wayboll_close').click(function () {
        hideOverlay();//关闭遮罩层
        $('.add_wayboll').hide();
        clearAddDiv();//清理输入控件残余值

    });

    clearAddDiv = function () {
        $('#txtShipNum').val('');
        $('#txtShipQuantity').val('');
        $('#txtShipImeiNums').val('');
        $('#txtRemark').val('');
    }
    //以上是添加货运单的js代码


    //展开的时候显示维修单号所属的来货单列表 begin
    loadWaybill = function (orNum, id, qty) {
        var uri = '<?= blang_url('orders/Multi/ajaxGetWaybillByOrderNum')?>';
        var lload;

        $.ajax({
            type: "POST",
            url: uri,
            async: false,
            data: {
                'orderNum': orNum
            },
            beforeSend: function (XMLHttpRequest) {
                lload = layer.load(1, {shade: [0.1, '#fff']});
            },
            complete: function (XMLHttpRequest, textStatus) {
                // alert('远程调用成功，状态文本值：'+textStatus);
                layer.close(lload);
            },
            dataType: "json",
            success: function (data) {
                if (data.length > 0) {
                    var list;
                    var subtotalqty = 0;
                    for (var i = 0; i < data.length; i++) {
                        subtotalqty = subtotalqty + parseInt(data[i].quantity);//累加数量
                        if (data[i].mp_num != '' && data[i].mp_num != null) {
                            sele = "display:none"
                        } else {
                            sele = "";
                        }
                        list += '<tr><td>' + (i + 1) + '</td><td><a href="javascript:void(0);" onclick=\'layer.alert("' + data[i].product_ids + '")\'>查看PID</a></td><td>' + data[i].waybill_num + '</td><td>' + data[i].quantity + '</td><td>' + data[i].add_time + '</td><td style="display:none;"><a href="javascript:void(0)">查看识别码</a> </td><td class="subtoteltd">' + (( subtotalqty / qty) * 100).toFixed(2) + '%</td><td><button class="layui-btn layui-btn-primary btnUnpk" style="' + sele + '" dt-on="' + data[i].order_num + '" dt-wn="' + data[i].waybill_num + '" dt-unid="' + data[i].mmuid + '" dt-unsort="' + data[i].unpk_cnt + '" dt-qty="' + data[i].quantity + '" dt-gqty="' + qty + '" dt-goid="' + id + '" dt-unpknum="' + data[i].unpackage_num + '"><i class="layui-icon">&#xe61d;</i> <s>拆分包裹</s></button><span class="layui-btn layui-btn-primary chkCheck" style="color: #333; ' + sele + '"><i class="layui-icon" style="margin-left:-10px;">&#xe618;</i> <s>选择</s> <input type="checkbox" class="cbkWaybill" name="cbkWaybill" value="' + data[i].quantity + '" dt-uid="' + data[i].mmuid + '" dt-tid="' + id + '" dt-oqty="' + qty + '" dt-unnum="' + data[i].unpackage_num + '" dt-ordernum="' + data[i].order_num + '"></span></td><tr>';
                    }
                    $('#tb_waybills_' + id).html(list);
                    setQtyAndAdd(id);

                    //重新绑定事件 begin
                    $(document).on("click", '.btnUnpk', function () {
                        //打开拆分的操作 begin
                        $('#main_unpk').show();
			$('#btnMergeReset').trigger('click');
			// $('#main_merge').hide();
                        //弹出层定位
                        $('#main_unpk').css('top', $(this).position().top);
                        $('#main_unpk').css('left', $(this).position().left - 250);
                        //设置数量
                        $("#hidPkQty").val($(this).attr('dt-qty'));
                        $('#btnUnpkQty').val($(this).attr('dt-qty'));

                        $('#hidPkId').val($(this).attr('dt-unid'));
                        $('#hidPkSort').val($(this).attr('dt-unsort'));
                        $('#hidOnum').val($(this).attr('dt-on'));
                        $('#hidUnpkum').val($(this).attr('dt-unpknum'));
                        $('#hidwNum').val($(this).attr('dt-wn'));

                        $('#hidGqty').val($(this).attr('dt-gqty'));
                        $('#hidGoid').val($(this).attr('dt-goid'));

                        $('#btnUnpkReQty').val(0);
                        //打开拆分的操作 end
                    });
                    //重新绑定事件 end
                    cbkcheckkk();
                } else {
                    $('#tb_waybills_' + id).html('<tr><td colspan="8"><strong>抱歉，暂时没有来货单！</strong></td></tr>');
                }
                layer.close(lload);
            }
        });
    }
    //展开的时候显示物流单号的产品列表 end


    //弹出合并包裹弹出层 begin
    $('.btn_merge_package').click(function () {
        var orderNum = $.trim($(this).attr('data-on'));
        $('#hidOrderQty').val($(this).attr('data-qty'));
        showOverlay();
        $('.merge_package_div').show();
        loadMegePackage(orderNum);
    });
    //弹出合并包裹弹出层 end


    //关闭合并包裹 begin
    $('.add_wayboll_close').click(function () {
        hideOverlay();//关闭遮罩层
        $('.merge_package_div').hide();

    });
    //关闭合并包裹 end

    //加载合并包裹 begin
    loadMegePackage = function (orderNum) {
        var uri = '<?= blang_url('orders/Multi/ajaxGetWaybillByOrderNum')?>';
        var lload;
        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'orderNum': orderNum
            },

            beforeSend: function (XMLHttpRequest) {
                lload = layer.load(1, {shade: [0.1, '#fff']});

            },
            complete: function (XMLHttpRequest, textStatus) {
                layer.close(lload);
            },
            dataType: "json",
            success: function (data) {
                if (data.length > 0) {
                    var list;
                    var subtotalqty = 0;
                    for (var i = 0; i < data.length; i++) {

                        list += '<tr><td>' + data[i].waybill_num + '</td><td>' + data[i].quantity + '</td><td>' + data[i].add_time + '</td><td><button class="layui-btn layui-btn-primary btnUnpacking"><i class="layui-icon">&#xe61d;</i> <s>拆分包裹</s></button></td><tr>';
                    }
                    $('#tb_merge_package').html(list);

                } else {
                    $('#tb_merge_package').html('<tr><td colspan="4"><strong>抱歉，暂时没有来货单！</strong></td></tr>');
                }
                layer.close(lload);
            }
        });
    }

    //保存合并包裹 begin
    $('#btnMergeSave').click(function () {
        var cnts = '';//计数器
        var nums = '';
        var tti = 0;
        var ms_order_num = '';
        $('.cbkWaybill').each(function () {
            if ($(this).is(':checked')) {
                sp = ',';
                if (tti == 0) {
                    sp = '';
                    tti++;
                }
                cnts = cnts + sp + $(this).attr('dt-uid');
                nums = nums + sp + $(this).attr('dt-unnum');

            }
            ms_order_num = $(this).attr('dt-ordernum');
        });
        if (parseInt($('#btnMergeQty').val()) == 0) {
            layer.msg('亲，您没有选择合并的包裹哦');
            return;
        }
        var uri = '<?= blang_url('orders/Multi/ajaxMergePackage')?>';

        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'num': $('#txtMergeNum').val(),
                'order_num': ms_order_num,
                'unpk_ids': cnts,
                'unpk_nums': nums
            },
            dataType: "text",
	    beforeSend:function(){
	    	$('#btnMergeSave').attr("disabled","disabled");
	    	$('#btnMergeSave').text("合并中...");
	    },
            success: function (data) {
                layer.msg('合并成功');
			$('#btnMergeSave').removeAttr("disabled");
				$('#btnMergeSave').text("合并");
                $('#main_merge').hide();//关闭弹出层
                loadWaybill($('#hidMMNum').val(), $('#hidMMId').val(), $('#hidMMQty').val());//ajax加载来货单

            }
        });


    });
    //保存合并包裹 end

    //关闭合并包裹 begin
    $('#btnMergeReset').click(function () {
        $('#main_merge').hide();
        $('#txtMergeNum').val('');

        //如果已经被选中
        if ($('.chkCheck').find("input[type='checkbox']").is(':checked')) {
            //如果还没有没选中
            $('.chkCheck').css('background-color', '#fff');
            $('.chkCheck').css('color', '#333');
            $('.chkCheck').find("input[type='checkbox']").prop('checked', false);

        }
    });
    //关闭合并包裹 end


    //加载合并包裹 end

    //拆包 begin
    //拆包按钮事件 begin
    $('#btnUnpkSave').click(function () {
        if (parseInt($.trim($('#btnUnpkReQty').val())) == 0 || parseInt($.trim($('#btnUnpkQty').val())) == 0) {
            layer.msg("亲，这样玩有意思么？0还拆分个啥啊？");
            return;
        }
        var uri = '<?= blang_url('orders/Multi/ajaxAddUnpackage')?>';
        var lload;
        $.ajax({
            type: "POST",
            async: false,
            url: uri,
            data: {
                'orderNum': $('#hidOnum').val(),
                'uid': $('#hidPkId').val(),
                'num': $('#hidwNum').val(),
                'uncnt': $('#btnUnpkQty').val(),
                'recnt': $('#btnUnpkReQty').val(),
                'sort': $('#hidPkSort').val(),
                'unpkNum': $('#hidUnpkum').val()
            },
            dataType: "text",
            success: function (data) {
                layer.msg('拆包成功');
                closeUnpkDiv();//关闭按钮
                loadWaybill($('#hidOnum').val(), $('#hidGoid').val(), $('#hidGqty').val());//ajax加载来货单
            }
        });
    });
    //拆包按钮事件 end

    //关闭拆包
    $('#btnUnpkReset').click(function () {
        closeUnpkDiv();
    });
    $('#btnUnpkQty').keyup(function () {
        var qqty = parseInt($('#hidPkQty').val());
        if ($.trim($('#btnUnpkQty').val()) == '') {
            $('#btnUnpkQty').val(0);
            $('#btnUnpkReQty').val(qqty);
            return;
        }
        if (parseInt($('#btnUnpkQty').val()) > qqty) {
            layer.msg('超标了，亲');
            $('#btnUnpkQty').val(qqty);
            $('#btnUnpkReQty').val(0);
        } else {
            $('#btnUnpkReQty').val(qqty - parseInt($('#btnUnpkQty').val()));
        }
    });
    closeUnpkDiv = function () {
        $('.main_unpk').hide();
        $('#btnUnpkQty').val('');
        $('#btnUnpkReQty').val('');
    }
    cbkcheckkk = function () {
        //合并包裹 选择特效 begin
        $('.chkCheck').click(function () {

            $('#hidMMNum').val($(this).find("input[type='checkbox']").attr('dt-ordernum'));
            $('#hidMMId').val($(this).find("input[type='checkbox']").attr('dt-tid'));
            $('#hidMMQty').val($(this).find("input[type='checkbox']").attr('dt-oqty'));
            //如果已经被选中
            if ($(this).find("input[type='checkbox']").is(':checked')) {
                //如果还没有没选中
                $(this).css('background-color', '#fff');
                $(this).css('color', '#333');
                $(this).find("input[type='checkbox']").prop('checked', false);

            } else {
                //如果还没有没选中
                $(this).css('background-color', '#1aa8f4');
                $(this).css('color', '#fff');
                $(this).find("input[type='checkbox']").prop('checked', 'checked');
            }
            var cnt = 0;//计数器
            $('.cbkWaybill').each(function () {
                if ($(this).is(':checked')) {
                    cnt = cnt + parseInt($(this).val());
                }
            });
            var oqty = 0;// $(this).find("input[type='checkbox']").attr('dt-oqty');
            //重新计算
            $(this).parent().parent().parent().find("input[type='checkbox']").each(function () {
                var disp = ($(this).parent('span').css('display'));
                if (disp != 'none') {//隐藏的不再计算之内部
                    oqty = parseInt($(this).val()) + oqty;
                }


            });
            //重新计算end
            // alert(oqty);
            $('#btnMergeQty').val(cnt);
            $('#btnMergeReQty').val(oqty - cnt);
            //行内弹出合并包裹 begin
            var ttmegre = $('#waybill_tb_' + $(this).find("input[type='checkbox']").attr('dt-tid'))
            $('#main_merge').css('top', ttmegre.position().top + 60);
            $('#main_merge').css('right', $(this).find("input[type='checkbox']").position().left + 210);
            $('#main_merge').show();
	    $('#main_unpk').hide();
            //行内弹出合并包裹 end


            //设置合并包裹号
            var timestamp = new Date().getTime();
            $('#txtMergeNum').val(timestamp);


        });
        //合并包裹 选择特效 end
    }
    //拆包 end

    //设置订单百分比以及添加按钮的显示与隐藏 begin
    setQtyAndAdd = function (id) {
        var pp = $('#waybill_tb_' + id).find('.subtoteltd').last().text().replace('%', '');
        if (pp > 100) {
            $('#per_' + id).css("color", "#db3652");
            $('#per_' + id).css("font-weight", "bold");
        }
        $('#per_' + id).text($('#waybill_tb_' + id).find('.subtoteltd').last().text());
    }
    //设置订单百分比以及添加按钮的显示与隐藏 end


    /* 显示遮罩层 */
    function showOverlay() {
        $("#overlay").height(pageHeight());
        $("#overlay").width(pageWidth());
        // fadeTo第一个参数为速度，第二个为透明度
        // 多重方式控制透明度，保证兼容性，但也带来修改麻烦的问题
        $("#overlay").fadeTo(200, 0.5);
    }

    /* 隐藏覆盖层 */
    function hideOverlay() {
        layer.closeAll();
        $("#overlay").fadeOut(200);
    }

    /* 当前页面高度 */
    function pageHeight() {
        return document.body.scrollHeight;
    }

    /* 当前页面宽度 */
    function pageWidth() {
        return document.body.scrollWidth;
    }

    /**/


</script>

